#include "listitembox.h"
#include "ui_listitembox.h"

#include <QDebug>

ListItemBox::ListItemBox(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ListItemBox),
    isLike(false)
{
    ui->setupUi(this);

    // 隐藏lineBoxbg控件
    ui->lineBoxbg->hide();

    // 设置line1的动画效果
    line1Animal = new QPropertyAnimation(ui->line1, "geometry", this);
    line1Animal->setDuration(1500);
    line1Animal->setKeyValueAt(0, QRect(0, 6, 3, 9));  // 根据ui->line1的geometry属性设置
    line1Animal->setKeyValueAt(0.2, QRect(0, 0, 3, 15));
    line1Animal->setKeyValueAt(0.7, QRect(0, 15, 3, 0));
    line1Animal->setKeyValueAt(1, QRect(0, 6, 3, 9));
    line1Animal->setLoopCount(-1);
    line1Animal->start();

    // 设置line2的动画效果
    line2Animal = new QPropertyAnimation(ui->line2, "geometry", this);
    line2Animal->setDuration(1500);
    line2Animal->setKeyValueAt(0, QRect(6, 15, 3, 0));
    line2Animal->setKeyValueAt(0.5, QRect(6, 0, 3, 15));
    line2Animal->setKeyValueAt(1, QRect(6, 15, 3, 0));
    line2Animal->setLoopCount(-1);
    line2Animal->start();

    // 设置line3的动画效果
    line3Animal = new QPropertyAnimation(ui->line3, "geometry", this);
    line3Animal->setDuration(1500);
    line3Animal->setKeyValueAt(0, QRect(12, 9, 3, 6));
    line3Animal->setKeyValueAt(0.3, QRect(12, 0, 3, 15));
    line3Animal->setKeyValueAt(0.8, QRect(12, 15, 3, 0));
    line3Animal->setKeyValueAt(1, QRect(12, 9, 3, 6));
    line3Animal->setLoopCount(-1);
    line3Animal->start();

    // 设置line4的动画效果
    line4Animal = new QPropertyAnimation(ui->line4, "geometry", this);
    line4Animal->setDuration(1500);
    line4Animal->setKeyValueAt(0, QRect(18, 0, 3, 15));
    line4Animal->setKeyValueAt(0.5, QRect(18, 15, 3, 0));
    line4Animal->setKeyValueAt(1, QRect(18, 0, 3, 15));
    line4Animal->setLoopCount(-1);
    line4Animal->start();
}

ListItemBox::~ListItemBox()
{
//    qDebug() << "ListItemBox被销毁";
    delete ui;
}

void ListItemBox::setMusicName(const QString &musicName)
{
    ui->musicNameLabel->setText(musicName);
}

void ListItemBox::setMusicSinger(const QString &musicSinger)
{
    ui->musicSingerLabel->setText(musicSinger);
}

void ListItemBox::setMusicAlbum(const QString &musicAlbum)
{
    ui->musicAlbumLabel->setText(musicAlbum);
}

void ListItemBox::setDuration(const QString &duration)
{
    ui->musicDurationLabel->setText(duration);
}

void ListItemBox::setCoverImage(const QPixmap &image)
{
    ui->musicImageBox->setPixmap(image);
    ui->musicImageBox->setScaledContents(true);
//    setStyleSheet("#musicImageBox{border-radius: 15px;}");
}

void ListItemBox::setIsLikeMusic(bool isLike)
{
    this->isLike = isLike;
//    qDebug() << "是否喜欢：" << isLike;
    if (isLike)
    {
//        ui->likeBtn->setIcon(QIcon(":/images/like_solid.png"));
        ui->likeBtn->setStyleSheet("#likeBtn { background-image: url(:/images/like_solid.png); } #likeBtn:hover { background-image: url(:/images/like_solid-hover.png); }");
    }
    else
    {
//        ui->likeBtn->setIcon(QIcon(":/images/like_hollow_light.png"));
        ui->likeBtn->setStyleSheet("#likeBtn { background-image: url(:/images/like_hollow_light.png); } #likeBtn:hover { background-image: url(:/images/like_hollow-hover); }");
    }
}

void ListItemBox::setItemStyle(bool isPlaying, bool onlyAnimal)
{
    if (isPlaying)
    {
        ui->lineBoxbg->show();
        if (!onlyAnimal) this->setStyleSheet("#musicNameLabel, #musicSingerLabel, #musicAlbumLabel, #musicDurationLabel{color: rgb(0, 204, 101);}");
    }
    else
    {
        ui->lineBoxbg->hide();
        if (!onlyAnimal) this->setStyleSheet("#musicNameLabel, #musicSingerLabel, #musicAlbumLabel, #musicDurationLabel{color: rgb(0, 0, 0);}");
    }
}

void ListItemBox::on_likeBtn_clicked()
{
//    // 修改当前图标
//    setIsLikeMusic(!isLike);
//    qDebug() << "listitembox收到点击喜欢按钮的信号，发信号给CommonPage";

    // 收藏状态改变，发出通知，修改Music对象里的isLike属性
    emit setIsLike(!isLike);
}
